package com.sy.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.dto.CoursePageQueryDTO;
import com.sy.dto.CoursedetailPageQueryDTO;
import com.sy.pojo.Course;
import com.sy.pojo.CourseType;
import com.sy.pojo.CourseUser;
import com.sy.pojo.Coursedetail;
import com.sy.result.PageResult;
import com.sy.result.Result;
import com.sy.service.CourseService;
import com.sy.service.CourseTypeService;
import com.sy.service.CourseUserService;
import com.sy.service.CoursedetailService;
import com.sy.utils.FastDFSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "课程明细管理相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/courseDetail")
public class CoursedetailController {
    @Autowired
    private CoursedetailService coursedetailService;
    //课程查询
    @PostMapping("/save")
    @ApiOperation("课程明细的添加")
    public Result<String> save(@RequestBody Coursedetail coursedetail){
        log.info("课程明细添加：{}",coursedetail);
        try {
            coursedetail.setCreateTime(LocalDateTime.now());
            coursedetailService.save(coursedetail);
            return  Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException("课程明细添加失败");
        }
    }
    //课程查询
    @GetMapping("/findAll")
    @ApiOperation("课程明细的删除  ")
    public Result<PageResult> findAll(CoursedetailPageQueryDTO coursedetailPageQueryDTO){
        log.info("课程明细分页查询：{}",coursedetailPageQueryDTO);
        //调用业务获取数据
        PageResult pageResult=coursedetailService.findAll(coursedetailPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/delete")
    @ApiOperation("课程明细的删除")
    public Result<String> delete(Integer id){
        log.info("课程明细的删除：{}",id);
        try {
            //调用业务获取数据
            Coursedetail coursedetail=coursedetailService.getById(id);
            //删除课程明细
            coursedetailService.removeById(id);
            //删除明细对应的视频
            FastDFSClient.deleteFile(coursedetail.getUrl());
            return Result.success();
        } catch (Exception e) {
            throw new RuntimeException("课程明细删除失败");
        }

    }
}
